package cn.wen.bases.framework.struts.interceptor;

import org.apache.log4j.Logger;

import cn.wen.java.utils.HelperOfDate;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * Title：总处理时长（纳秒级）拦截器
 * <p>
 * Description：
 * <p>
 * Copyright：Copyright (c) 2010-2012
 * 
 * @author <a href="mailto:ct_wen@qq.com">ctwen</a>, 温承瑭
 * @version Revision：1.0 Date：2010/11/15 Description：初完成。
 *          <p>
 */
public class NanoLevelInterceptor extends AbstractInterceptor {

    private static final long serialVersionUID = 725529192138756208L;

    private Logger log = Logger.getLogger(getClass());

    /**
     * <pre>
     * 输出请求处理时长（纳秒级）。
     * </pre>
     * 
     * @see com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
     */
    @Override
    public String intercept(ActionInvocation ai) throws Exception {
	// 程序片段启动时间
	// System.out.println("Start time :\t\t"+HelperOfDate.formatPrecisionTime(beforeNs));
	long beforeNs = System.nanoTime();

	String result = ai.invoke();

	// 程序片段完成时间
	// System.out.println("Done time :\t\t"+HelperOfDate.formatPrecisionTime(afterNs));
	long afterNs = System.nanoTime();

	// 程序片段从启动到完成的时差
	long nsd = afterNs - beforeNs;
	String str = "Time difference: " + HelperOfDate.formatNanoTime(nsd);
	System.out.println(str);
	log.info("*********************************************************************************************************************************************************\t"
		+ str);
	return result;
    }

}
